import VueRouter from 'vue-router'
import Vue from 'vue'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
Vue.use(VueRouter)
const childMenu = [
    {
        path: '/',
        name: '首页',
        component: () => import('../views/home.vue'),
    },
    {
        path: '/system/userList',
        name: '用户管理',
        component: () => import('../views/system/userList.vue'),
    },
    {
        path: '/system/userAdd',
        name: '新增用户',
        component: () => import('../views/system/userAdd.vue'),
    },
    {
        path: '/demoForm',
        name: '表单',
        component: () =>
            import('../views/demo/form.vue'),
    },
    {
        path: '/demoList',
        name: '表格',
        component: () =>
            import('../views/demo/list.vue'),
    },
    {
        path: '/demoDialog',
        name: '弹窗',
        component: () =>
            import('../views/demo/dialog.vue'),
    },
];
    
const routes = [
    {
        path: '/',
        name: '',
        meta:{
            requireLogin:true
        },
        component: () =>
            import('../views/index.vue'),
        children:childMenu
        
    },
    {
        path: '/login',
        name: '登录',
        component: () =>
            import('../views/login.vue'),
    },
    
    {
        path: '/idiom',
        name: '成语词典',
        component: () =>
            import('../views/searchData/idiom.vue'),
    },
]
const router = new VueRouter({
  routes,
  mode: 'hash',
})

//路由守卫
router.beforeEach((to, from, next) => {
     // 进度条
     NProgress.start() // 显示进度条
     NProgress.set(0.4) // 设置百分比
     NProgress.inc() // 稍微增加
    if (to.matched.some(record => record.meta.requireLogin)) {
            let token = localStorage.getItem('token');
            if (!token) {
                next({ path: '/login'});
            } else {
                next();
            }
        
    } else {
        next()
    }

})
router.afterEach(() => {
    NProgress.done();//完成进度(进度条消失)
});

export default router